package Cart_Function;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import Cart_Utils.Cart;
import Goods_Utils.Good;
import JDBC.JDpoor;

public class Cart_Function {
	Connection con = new JDpoor().CreateCon();;
	Statement sql = null;
	PreparedStatement pre = null;
	ResultSet rs = null;
	boolean f = false;
	
	public boolean hasStock(String name,String num){
		String c = "select stock from goods_info where name=?";
		try {
			pre = con.prepareStatement(c);
			pre.setString(1, name);
			rs = pre.executeQuery();
			if(rs.next()){
				int stock = Integer.parseInt(rs.getString("stock"));
				if(Integer.parseInt(num)<=stock){
					f = true;
				}else
					f = false;
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
			f = false;
		}
		return f;
	}
	
	public boolean addGood(Cart c) {
		String condition;
		if(hasStock(c.getName(), c.getNum())){
			if(hasCart(c.getName(),c.getUser())){
				String num = getNum(c.getName(),c.getUser())+Integer.parseInt(c.getNum())+"";
				String total = (Integer.parseInt(num)*Integer.parseInt(c.getPrice()))+"";
				condition = "update cart_info set num=?,total=? where name=? and user=?";
				try {
					pre = con.prepareStatement(condition);
					pre.setString(1, num);
					pre.setString(2, total);
					pre.setString(3, c.getName());
					pre.setString(4, c.getUser());
					int t = pre.executeUpdate();
					if(t!=0){
						f = true;
					}else
						f = false;
				} catch (Exception e) {
					// TODO: handle exception
					f = false;
					System.out.println(e);
				}
			}else{
				condition = "insert into cart_info values(?,?,?,?,?,?)";
				try {
					pre = con.prepareStatement(condition);
					pre.setString(1, c.getName());
					pre.setString(2, c.getNum());
					pre.setString(3, c.getPrice());
					pre.setString(4, c.getTotal());
					pre.setString(5, c.getUser());
					pre.setString(6, c.getUrl());
					int t = pre.executeUpdate();
					if(t!=0){
						f = true;
					}else
						f = false;
					
				} catch (Exception e) {
					// TODO: handle exception
					System.out.println(e);
					f = false;
				}
			}
		}else
			f = true;
		return f;
	}

	private int getNum(String name, String user) {
		// TODO Auto-generated method stub
		String condition = "select num from cart_info where name=? and user=?";
		int num = 0;
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, name);
			pre.setString(2, user);
			rs = pre.executeQuery();
			while(rs.next()){
				num = Integer.parseInt(rs.getString("num"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
		return num;
	}

	private boolean hasCart(String name, String user) {
		// TODO Auto-generated method stub
		String condition = "select * from cart_info where name=? and user=?";
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, name);
			pre.setString(2, user);
			rs = pre.executeQuery();
			if(rs.next()){
				f = true;
			}else
				f = false;
		} catch (Exception e) {
			// TODO: handle exception
			f = false;
			System.out.println(e);
		}
		return f;
	}

	public Cart creatCart(String name, String num, String user) {
		// TODO Auto-generated method stub
		String c = "select * from goods_info where name=?";
		Cart cart = new Cart();
		
		try {
			pre = con.prepareStatement(c);
			pre.setString(1, name);
			rs = pre.executeQuery();
			while(rs.next()){
				cart.setName(name);
				cart.setUser(user);
				cart.setNum(num);
				cart.setPrice(rs.getString("price"));
				cart.setUrl(rs.getString("url"));
				String total = (Integer.parseInt(num)*Integer.parseInt(rs.getString("price")))+"";
				cart.setTotal(total);
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
		return cart;
	}

	public boolean empty(String user) {
		// TODO Auto-generated method stub
		String condition = "delete from cart_info where user=?";
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, user);
			int t = pre.executeUpdate();
			if(t!=0){
				f = true;
			}else{
				f = false;
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("empty出错："+e);
			f = false;
		}
		return f;
	}

	public ArrayList<Cart> getUserCart(String user) {
		// TODO Auto-generated method stub
		ArrayList<Cart> carts = new ArrayList<Cart>();
		
		String condition = "select * from cart_info where user=?";
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, user);
			rs = pre.executeQuery();
			while(rs.next()){
				Cart cart = new Cart();
				cart.setName(rs.getString("name"));
				cart.setNum(rs.getString("num"));
				cart.setTotal(rs.getString("total"));
				cart.setPrice(rs.getString("price"));
				cart.setUser(user);
				cart.setUrl(rs.getString("url"));
				carts.add(cart);
			}
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
		return carts;
	}

	public void DeleteCart(String name,String user) {
		// TODO Auto-generated method stub
		String condition = "delete from cart_info where name=? and user=?";
		
		try {
			pre = con.prepareStatement(condition);
			pre.setString(1, name);
			pre.setString(2, user);
			pre.executeUpdate();
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
	}
	
}
